#include "stdafx.h"

#include "..\FindFile.h"

namespace Slow
{
TESTCLASS(FindFileTests)
{
	CPath m_path;
public:
	FindFileTests()
		: m_path(InitializePath())
	{}

private:
	static CPath InitializePath()
	{
		CString csFilename;
		::GetModuleFileName(_AtlBaseModule.GetModuleInstance(), csFilename.GetBuffer(MAX_PATH), MAX_PATH);
		csFilename.ReleaseBuffer();

		CPath path(csFilename);
		path.RemoveFileSpec();
		return path;
	}

private:
	TESTMETHOD(FindCurrentTestDllAsFile)
	{
		CFindFile ff;
		TDD_VERIFY(TRUE == ff.FindFile(CString(m_path) + L"\\*.dll"));
		TDD_VERIFY(ff.GetFileName() == L"TddConsoleUnitTests.dll");
		TDD_VERIFY(ff.GetFilePath() == CString(m_path) + L"\\TddConsoleUnitTests.dll");
		TDD_VERIFY(!ff.IsReadOnly());
		TDD_VERIFY(!ff.IsCompressed());
		TDD_VERIFY(!ff.IsSystem());
		TDD_VERIFY(!ff.IsHidden());
		TDD_VERIFY(!ff.IsTemporary());
		TDD_VERIFY(!ff.IsNormal());
		TDD_VERIFY(ff.IsArchived());
	}

	TESTMETHOD(FailToFindAnotherDll)
	{
		CFindFile ff;
		ff.FindFile(CString(m_path) + L"\\*.dll");
		TDD_VERIFY(FALSE == ff.FindNextFile());
	}

	TESTMETHOD(FindDirectory)
	{
		CFindFile ff;
		TDD_VERIFY(TRUE == ff.FindFile(CString(m_path)));
		TDD_VERIFY(ff.IsDirectory());
	}

	TESTMETHOD(FindAllFindsDotsFirst)
	{
		CFindFile ff;
		TDD_VERIFY(TRUE == ff.FindFile(CString(m_path) + L"\\*.*"));
		TDD_VERIFY(TRUE == ff.IsDirectory());
		TDD_VERIFY(TRUE == ff.IsDots());
		TDD_VERIFY(L"." == ff.GetFileName());
		TDD_VERIFY(TRUE == ff.FindNextFile());
		TDD_VERIFY(TRUE == ff.IsDirectory());
		TDD_VERIFY(TRUE == ff.IsDots());
		TDD_VERIFY(L".." == ff.GetFileName());
	}

	TESTMETHOD(FailToFindImpossibleFile)
	{
		CFindFile ff;
		TDD_VERIFY(FALSE == ff.FindFile(L"c:\\impossible*\\directory\\"));
		TDD_VERIFY(L"" == ff.GetFileName());
	}

	TESTMETHOD(FindNextFileFailsIfFileFileNotCalledFirst)
	{
		CFindFile ff;
		TDD_VERIFY(FALSE == ff.FindNextFile());
		TDD_VERIFY(FALSE == ff.IsArchived());
	}

	TESTMETHOD(PathTooLongReturnsFalse)
	{
		CFindFile ff;
		TDD_VERIFY(FALSE == ff.FindFile(
L"0123456789012345678901234567890123456789"
L"0123456789012345678901234567890123456789"
L"0123456789012345678901234567890123456789"
L"0123456789012345678901234567890123456789"
L"0123456789012345678901234567890123456789"
L"0123456789012345678901234567890123456789"
L"0123456789012345678901234567890123456789"
L"01234567890123456789"));
	}

	TESTMETHOD(DefaultParameterIsAllInCurrentDirectory)
	{
		class ResetCWD
		{
			CString m_cwd;
		public:
			ResetCWD(const CString& path)
			{
				::GetCurrentDirectory(MAX_PATH, m_cwd.GetBuffer(MAX_PATH+1));
				m_cwd.ReleaseBuffer();

				::SetCurrentDirectory(path);
			}
			~ResetCWD()
			{
				::SetCurrentDirectory(m_cwd);
			}
		} cwd(m_path);

		CFindFile ff;
		TDD_VERIFY(TRUE == ff.FindFile());
		TDD_VERIFY(TRUE == ff.IsDots());
		TDD_VERIFY(CString(m_path) == ff.GetRoot());
	}
};
}
